package com.jie.server.mvc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.common.http.constant.HttpEntityClassNameConstant;
import com.jie.common.http.constant.HttpHeaderLineNameConstant;
import com.jie.common.http.constant.response.ServerResponseStatusConstant;
import com.jie.common.http.entity.request.HttpRequest;
import com.jie.common.http.entity.response.HttpResponse;
import com.jie.common.util.JsonUtil;
import com.jie.server.core.annotation.Mapping;
import com.jie.server.mvc.model.entity.SingleChat;
import com.jie.server.mvc.model.vo.SingleChatQueryVo;
import com.jie.server.mvc.service.SingleChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 单聊表 前端控制器
 * </p>
 *
 * @author XieZhiJie
 * @since 2022-04-24
 */
@Controller("single-chat")
public class SingleChatController {

    public final SingleChatService singleChatService;

    @Autowired
    public SingleChatController(
            SingleChatService singleChatService
    ) {
        this.singleChatService = singleChatService;
    }

    /**
     * 发送一条信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse chat(HttpRequest httpRequest, HttpResponse httpResponse) {
        SingleChat singleChat = JsonUtil.fromJson(httpRequest.getEntityString(), SingleChat.class);
        // 设置发送者用户id
        singleChat.setSendUserId(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.singleChatService.chat(singleChat)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_ONE_SUCCESS);
            httpResponse.setEntity("发送成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_ONE_FAIL);
            httpResponse.setEntity("发送失败!");
        }
        return httpResponse;
    }

    /**
     * 获取到聊天记录
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse records(HttpRequest httpRequest, HttpResponse httpResponse) {
        SingleChatQueryVo singleChatQueryVo = JsonUtil.fromJson(httpRequest.getEntityString(), SingleChatQueryVo.class);
        Page<SingleChat> singleChatPage = this.singleChatService.records(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)), singleChatQueryVo);
        httpResponse.addOneHeaderLine("total", String.valueOf(singleChatPage.getTotal()));
        httpResponse.setEntity(JsonUtil.toJson(singleChatPage.getRecords()));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_ONE_RECORDS_SUCCESS);
        return httpResponse;
    }

}
